package com.zefor.form;


import com.fasterxml.jackson.databind.ser.std.StdKeySerializers;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.dataentity.entity.LocaleString;
import kd.bos.entity.datamodel.events.ChangeData;
import kd.bos.entity.datamodel.events.IDataModelChangeListener;
import kd.bos.entity.datamodel.events.PropertyChangedArgs;
import kd.bos.form.IFormView;
import kd.bos.form.control.Control;
import kd.bos.form.control.events.BeforeClickEvent;
import kd.bos.form.field.BasedataEdit;
import kd.bos.form.field.ComboEdit;
import kd.bos.form.field.ComboItem;
import kd.bos.form.field.events.BeforeF7SelectEvent;
import kd.bos.form.field.events.BeforeF7SelectListener;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.list.BillList;
import kd.bos.orm.ORM;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.tmc.cfm.common.helper.BusinessHelper;
import org.hsqldb.lib.ArrayUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.EventObject;
import java.util.List;

public class EnvironmentalManagementForm extends AbstractFormPlugin {

    // orm数据库操作工具
    private final ORM orm = ORM.create();

    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);
    }

    @Override
    public void propertyChanged(PropertyChangedArgs e) {
        String name = e.getProperty().getName();
        //自动填写设备类型
        if ("n716_basedatafield".equals(name)) {
            DynamicObject basedata = (DynamicObject) this.getModel().getValue("n716_basedatafield");
            if (basedata == null) {
                return;
            }
            QFilter qFilter = new QFilter("number", QCP.equals, basedata.get(1));
            DynamicObject dynamicObject = orm.queryOne("n716_equipmentlink", "id,n716_materielfield", qFilter.toArray());
            Long fid = (Long) dynamicObject.get(19);
            QFilter linkFilter = new QFilter("id", QCP.equals, fid);
            DynamicObject link = orm.queryOne("bd_material", "id,name", linkFilter.toArray());

            ComboEdit comboEdit = this.getControl("n716_combofield");
            List<ComboItem> comboItemList = new ArrayList<>();
            ComboItem comboItem = new ComboItem();
            comboItem.setCaption(new LocaleString(link.get(2).toString()));
            comboItem.setValue(link.get(2).toString());
            comboItemList.add(comboItem);
            comboEdit.setComboItems(comboItemList);

            this.getModel().setValue("n716_combofield", link.get(2));
        }
    }
}
